jQuery(function($){
	/*------------- layout resize----------------*/
	var hight =$(window).height();
	var widthScreen =$(window).width();
	var _thisToggleWestSelect =$('#toggle-west');
	var heightHeader = $('#header-container').outerHeight(true);
	var heightContainerChild = hight - heightHeader;
	var container = $('.hasleft-layout');
	var center = $('.hasleft-layout .center');
	var west = $('.hasleft-layout .west');
	var footerLeft = $('#footer-left');
	var _top_List_Action_Center = center.find('.top-list-action');
	var _main_Content = center.find('.main-content');
	var height_Main_content_scrl = heightContainerChild-_top_List_Action_Center.outerHeight(true);
	_main_Content.height(550);//height_Main_content_scrl);

	$('#content-container').height(550); //heightContainerChild);
	//if(lServerName == "") lServerName = "http://localhost:8080";
	//var url = lServerName + '/OpenLayers/jquery/jquery-ui-1.9.2.custom.min.js';
	//$.getScript(url, function() {
	$(function($){
		var maxwidth =container.width()-805;
		//fix heigh indicator .end
		if(widthScreen > 1024){
			function relayout() {
				container.layout({resize: false});
			}
			relayout();

			$(window).resize(relayout);
			$('.south').resizable({
				handles: 'n',
				stop: relayout
			});
			$('.west').resizable({
				handles: 'e',
				stop: relayout,
				minWidth:299,
				maxWidth: maxwidth
			});
			_thisToggleWestSelect.click(function () {
				$(this).toggleClass("toggle-west-open");
				$('.west').animate({width: ['toggle', 'swing']}, {duration: 500, complete: relayout, step: relayout});
				MapAction.RefreshAction();
			});
		}else{
			$('#footer-left').width(299);
			$('.search-form-top .input').find('input').css({width:"95px"});
			$('#nav ul.dropdownMenu li a').css({padding:"7px 8px 6px"});
			_thisToggleWestSelect.click(function () {
				$(this).toggleClass("toggle-west-open");
				$('.west').animate({width: ['toggle', 'swing']}, {duration: 500});
				MapAction.RefreshAction();
			});
		}
		
		
	});
	
	/* toogle top header  */
	var parentHead = $(".header-top .user-account");
	var arrowHand = parentHead.find(".toggle-bt");
	var _thisNav = $("#nav");
	var _childNavBt = _thisNav.find(".toggle-nav-bt");
	arrowHand.click(function(){
		$(this).toggleClass("toggle-close-bt");
		$(this).parent().find(".toggle-login-ac").toggle("400");
	});
	_childNavBt.click(function(){
		$(this).toggleClass("toggle-nav-close-bt");
		$(this).parent().find(".dropdownMenu").toggle("300");
	});

		/* accordion menu tree*/
		var accordion = $(".accordion");
		var topWest = $(".top-west");
		var _accordionHeight = topWest.height();
		_accordionHeight = west.height()-footerLeft.outerHeight(true);
		topWest.height(_accordionHeight);
		/*var objLast = $(".accordion .para-none-scroll:last");
		alert("index = "+objLast.index());
		var last = objLast.offset();
		var footer = $("#footer-left").offset();
		alert("last.left = "+last.left+"\nlast.top = "+last.top+"\nfooter.top = "+footer.top);
		var distance = last.left-footer.top;
		alert("distance = "+distance+"\ndiv = "+distance/count);*/		
		accordion.find("h3").each(function (index, elem) {
			if ($(this).hasClass("ui-state-active")) {
				accordion.accordion({ fillSpace: true,active:index});
			}else{accordion.accordion({ fillSpace: true,active:false,change: 
				function(event, ui) { 
					heightnonescroll();
				}});
			}
		});
		heightnonescroll();
		/* height para content scroll limit */
		function heightnonescroll(){
			accordion.find(".para-none-scroll").each(function () {
				var _this = $(this);
				var _thisChildNonescroll = _this.find(".content-none-scroll");
				var sum = 0;
				 _thisChildNonescroll.each( function(){ sum += $(this).outerHeight(true);});
				var _thisChildScroll = _this.find(".hpcs");
				//var _thisChildScroll_2 =  _this.find(".hpcsn2");
				var _thisHeightChildScroll = _this.height()-sum+50;
				//var _thisHeightChildScroll_2 = _thisHeightChildScroll+37;
				_thisChildScroll.height(_thisHeightChildScroll);
				_this.height(_this.height()+30);
				//_thisChildScroll_2.height(_thisHeightChildScroll_2);
			});
		}
		/* tree view menu */
		$(".browser-tree").treeview();
		/* tooltip */
		$('.hover-tooltip-bt').tooltipster({ position: 'bottom'});
		$('.hover-tooltip-lt').tooltipster({ position: 'left'});
		$('.hover-tooltip-tt').tooltipster({ position: 'top'});
		$('.hover-tooltip-rt').tooltipster({ position: 'right'});


		/* drap map  */
		var $thisBlockMap = $('.map-block');
		$('.drag-left-map,.drag-right-map').drag(function( ev, dd ){
			dd.limit = $thisBlockMap.offset();
			dd.limit.bottom = $thisBlockMap.outerHeight(true) - $( this ).outerHeight(true);
			dd.limit.right = $thisBlockMap.outerWidth(true) - $( this ).outerWidth(true);
			$( this ).css({
				top: Math.min( dd.limit.bottom, Math.max( 0, dd.offsetY ) ),
				left: Math.min( dd.limit.right, Math.max( 0, dd.offsetX ) )
			});
		},{ relative:true },{ handle:".drag-handle" }); 

		/* tabs */
		$('.tabs').each(function(){
			$(this).find('.section').hide();

			var current = $(this).find('.item').children('.current');
			if (current.length == 0){
				$(this).find('.item').children(':first-child').addClass('current');
				$($(this).find('.item').children(':first-child').find('a').attr('href')).show();
			}else{
				$($(this).find('.item').children('.current').find('a').attr('href')).show();
			}

			$(this).find('.item').find('a').click(function(){
				var current = $(this).parent().hasClass('current');
				if (current == false){
					$(this).parent()
						.addClass('current')
						.siblings().each(function(){
							$(this).removeClass('current');
							$($(this).find('a').attr('href')).hide();
						});
					$($(this).attr('href')).fadeIn();
				}
				var sLayerID = "";
				var styleDefault = {
					    strokeColor: colorBorderPolygon,
					    strokeOpacity: "0.8",
					    strokeWidth: 2,
					    fillColor: colorSelectedFillPolygon,
					    fillOpacity: 0.8,
					    pointRadius: 5,
					    cursor: "pointer"
					};
				if($(this).attr('href') == "#div_Map_SplitFeatureAttributes_1")
				{
					sLayerID = assetTHUADAT_1.layerName;
					assetCurrent = assetTHUADAT_1;
					featureCurrentAction = splitOBJ.featureSplited[0];
					vectorGraphics_Search.removeAllFeatures();
					createPolygonFromGeometry(vectorGraphics_Search, featureCurrentAction.geometry, styleDefault);
				}
				else if($(this).attr('href') == "#div_Map_SplitFeatureAttributes_2")
				{
					sLayerID = assetTHUADAT_2.layerName;
					assetCurrent = assetTHUADAT_2;
					featureCurrentAction = splitOBJ.featureSplited[1];
					vectorGraphics_Search.removeAllFeatures();
					createPolygonFromGeometry(vectorGraphics_Search, featureCurrentAction.geometry, styleDefault);
				}
				else if($(this).attr('href') == "#div_Map_SplitFeatureAttributes_3")
				{
					sLayerID = assetTHUADAT_3.layerName;
					assetCurrent = assetTHUADAT_3;
					featureCurrentAction = splitOBJ.featureSplited[2];
					vectorGraphics_Search.removeAllFeatures();
					createPolygonFromGeometry(vectorGraphics_Search, featureCurrentAction.geometry, styleDefault);
				}
				else if($(this).attr('href') == "#div_Map_SplitFeatureAttributes_4")
				{
					sLayerID = assetTHUADAT_4.layerName;
					assetCurrent = assetTHUADAT_4;
					featureCurrentAction = splitOBJ.featureSplited[3];
					vectorGraphics_Search.removeAllFeatures();
					createPolygonFromGeometry(vectorGraphics_Search, featureCurrentAction.geometry, styleDefault);
				}
				//var bounds = new OpenLayers.Bounds(523527, 1776720, 523533, 1776732);
				//"SRID=900914;POLYGON((523533.803100001 1776732.1183,523532.2041 1776719.7563,523527.7681 1776720.3293,523529.3681 1776732.6935,523533.803100001 1776732.1183))"
				//map.zoomToExtent(bounds);				
				if (arrFieldAliasCurrent.length == 0) {
					var i = 0;
					for (i = 0; i < arrFieldAlias.length; i++) {
						if (base.trim(arrFieldAlias[i].LayerName.toLowerCase()) == base.trim(sLayerID.toLowerCase())) {
							arrFieldAliasCurrent[arrFieldAliasCurrent.length] = arrFieldAlias[i];
						}
					}
					arrFieldAliasCurrent.sort(function(a, b) {
						var x = a.Index - b.Index;
						return x;
					});
				}
				return false;
			});
		});
		
		$(function (){				
			loadXML();	

			$(".drag-map-content input").click(function() {
				LEVEL_TOOLBARITEM = $(this).attr('level');
				if(LEVEL_TOOLBARITEM> 0) $(this).addClass('active');
				$(this).siblings().each(function(){					
					var level = $(this).attr('level');
					if(level == LEVEL_TOOLBARITEM) $(this).removeClass('active');
				});
			});				
			
			$("#ul_Map_Congcuhotro li a").each(function()
			{
				var divContainer = $(this).attr('link');
				$(divContainer).hide();
			});
			
			$("#ul_Map_Congcuhotro li a").mousedown(function() {
				var divContainer = $(this).attr('link');
				$(divContainer).fadeIn(300);
				$(this).css('color','#ff0000');
				$(this).parent().siblings().each(function(){
					$(this).find('a').css('color','#000000');
					var divContainer = $(this).find('a').attr('link');
					$(divContainer).hide();
				});
			});	
			
			$("#ul_Map_Bandoquyhoach li a").mousedown(function() {
				$(this).css('color','#ff0000');
				$(this).parent().siblings().each(function(){
					$(this).find('a').css('color','#000000');
				});
			});	
			
			$("#ul_Map_Charts li a").mousedown(function() {
				var divContainer = $(this).attr('link');
				$(divContainer).fadeIn(300);
				$(this).css('color','#ff0000');
				$(this).parent().siblings().each(function(){
					$(this).find('a').css('color','#000000');
					var divContainer = $(this).find('a').attr('link');
					$(divContainer).hide();
				});
			});	
			
			$("#ul_Map_QuanlyDN li a").mousedown(function() {
				$(this).css('color','#0000ff');
				$(this).parent().siblings().each(function(){
					$(this).find('a').css('color','#000000');
				});
			});
			
			$('.tool-popup-content').parent().addClass('dragPopup');
			$('.dragPopup').drag(function( ev, dd ){
					if(actionCurrent == PortletTypeAction.Identify) return;
				   $( this ).css({
				    top: dd.offsetY,
				    left: dd.offsetX
				   });
				  },{ relative:true },{ handle:".tool-popup-content .h2-title-popup" });
			
			// load toan bo vung quy hoach chung
			$('#a_Map_Bandoquyhoach_AllVungQH').mousedown(function() {
				Cookies.Delete(QHCHUNG_COOKIE);
				window.location.reload();
			});
			$('#a_Map_Bandoquyhoach_AllVungQH').mouseover(function() {
				$(this).css('color', 'blue');
			});
			$('#a_Map_Bandoquyhoach_AllVungQH').mouseout(function() {
				$(this).css('color', '');
			});
			bookmark.map = map;		
			bookmark.List();		
			$('.form-mapleft-menu select').uniform();
			if(INDEX_MODULE == ListModule.quanlyMatdien) $('#toggle-west').click();

            //$('#map').data('editor').oleUrl = 'http://ole.geops.de:80/ole/';
			/*$('#breadcrumbs').hide();
			$('#navigation').hide();*/
			
			$(".toggle-location").click(function(){
				var state  = $(this).attr("state");
				if(state=="show"){
					$(this).removeClass('show_location');
					$(this).addClass('hidden_location');
					$(this).attr("state","hidden");
					$("#div_Map_divLocation").hide();
					$(this).attr("title",LANGUAGE["alias_location_show"]);
				}else{
					$(this).removeClass('hidden_location');
					$(this).addClass('show_location');
					$(this).attr("state","show");
					$("#div_Map_divLocation").show();
					$(this).attr("title",LANGUAGE["alias_location_hiden"]);
				}
			});
			
		});
		
		/* popup */
		$(".inline").colorbox({
				inline:true,
				width:"60%"
		});

		$(".popup-cl").click(function (e)
			{
			 ShowDialog(false);
			 e.preventDefault();
			});

			$("#btnClose").click(function (e)
			{
			 HideDialog();
			 e.preventDefault();
			});
		var _ShowHideContent = $('#btnShowHide');
		_ShowHideContent.click(function(){
			$(this).toggleClass("show-bt");
			$(this).parent().find(".show-hide-content").toggle("300");
		});	
		
		//add more thematic_div
		var _thematic_div = $(".thematic_div");
		var _map_div = $(".map_div");
		_thematic_div.hide();
		/*$(".thematic_load").click(function(){
			alert("1111");
			_map_div.hide();
			_thematic_div.show();
		});*/
		$(".come_back_map").click(function(){
			Is_LoadThematic_State = false;
			Is_LoadThematic_Type = false;
			Is_Active_ThematicAge = false;
			Is_Active_ThematicState = false;
			Is_Active_ThematicType = false;
			$("#UL_LIST_LI_THEMATIC_756 li").find("ul").each(function(){
				var obj = $(this);
				if(obj!=null)$(this).remove();
			});
			$("#ul_show_ListLegend_Type").hide();
			$("#ul_show_ListLegend_State").hide();
			_thematic_div.hide();
			_map_div.show();
		});
		
		$(".accordion h3.title-accordion").click(function(){
			Is_LoadThematic_State = false;
			Is_LoadThematic_Type = false;
			Is_Active_ThematicAge = false;
			Is_Active_ThematicState = false;
			Is_Active_ThematicType = false;
			$("#UL_LIST_LI_THEMATIC_756 li").find("ul").each(function(){
				var obj = $(this);
				if(obj!=null)$(this).remove();
			});
			$("#ul_show_ListLegend_Type").hide();
			$("#ul_show_ListLegend_State").hide();
			_thematic_div.hide();
			_map_div.show();
		});

		// on/off layer
		$(".top-list-action .devices-light li span").click(function() {
			var layername = $(this).attr('layername');
			layername = base.trim(layername.toLowerCase());
			var vectorItem = null;

			if(layername == "help")
			{
				//window.open("/AnGiang-557-portlet/html/giamsatmoitruong/help/TaiLieuHuongDanSudung_GSMT.htm", "blank");
				var myWindow = window.open("/AnGiang-557-portlet/html/giamsatmoitruong/help/index.html", "GSMT v0.9");
				return;
			}
			var indexLayerWFS = -1, indexLayerWMS = -1;
			// WFS - WMS
			for (var i = 0; i < vectorArray.length; i++) {
				vectorItem = vectorArray[i];
				if(layername == base.trim(vectorItem.LayerId.toLowerCase()))
				{				
					if(!vectorItem.Map_Service) indexLayerWFS = vectorItem.IndexLayer;
					else indexLayerWMS = vectorItem.IndexLayer;
				}
			}
			
			if($(this).hasClass('active')) 
			{
				$(this).removeClass('active');
				map.layers[indexLayerWMS].setVisibility(false);
				$(this).css('opacity', 0.3);
			}
			else 
			{
				$(this).addClass('active');
				map.layers[indexLayerWMS].setVisibility(true);
				$(this).css('opacity', 1);
			}
		});
		
		// thiet lap style table cua cac dialog
		var tblContentDialogs = $('.show-hide-content table');
		for(var i=0; i<tblContentDialogs.length;i++)
		{
			var tblContentDialog = tblContentDialogs[i];
			$(tblContentDialog).addClass('showHideContentTable');
		}	
		
		// return map
		$('#a_Map_ReturnMap').click(function() {
			//$('.' + thematicCurrent.classThematic).hide();
			//$('.' + thematicCurrent.classMap).show();
			chartObj.hideChart();
		});
    //});
	
});
//function ngoai
 function ShowDialog(modal)
   {
      $("#overlay").show();
      $("#popup_statistics").fadeIn(300);

      if (modal)
      {
         $("#overlay").unbind("click");
      }
      else
      {
         $("#overlay").click(function (e)
         {
            HideDialog();
         });
      }
   }

   function HideDialog()
   {
      $("#overlay").hide();
      $("#popup_statistics").fadeOut(300);
   }
